/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServicoSelector.java
 *
 * Created on Apr 9, 2009, 10:08:15 AM
 */

package doghost.selectors;

import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import doghost.controllers.ServicoController;
import doghost.entities.Servico;

/**
 *
 * @author duo
 */
public class ServicoSelector extends javax.swing.JDialog {

    protected ServicoController servicoController;
    protected Servico selectedServico;

    /** Creates new form ServicoSelector */
    public ServicoSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        servicoController = new ServicoController();
        servicoTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {


            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("servicoRecordSelected",
                                !isServicoRecordSelected(), isServicoRecordSelected());
                    }
                });
    }

    public Servico getSelectedServico() {
        return selectedServico;
    }

    public void setSelectedServico(Servico selectedServico) {
        this.selectedServico = selectedServico;
    }

    public boolean isServicoRecordSelected() {
        return servicoTable.getSelectedRow() != -1;
    }

    @Action
    public void newServico() {
        servicoController.newServico(doghostPUEntityManager, servicoList);
    }

    @Action(enabledProperty = "servicoRecordSelected")
    public void editServico() {
        int index = servicoTable.getSelectedRow();
        servicoController.setServico(servicoList.get(servicoTable.
                convertRowIndexToModel(index)));
        servicoController.editServico(doghostPUEntityManager, servicoList,
                servicoTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "servicoRecordSelected")
    public void deleteServico() {
        int index = servicoTable.getSelectedRow();
        servicoController.setServico(servicoList.get(servicoTable.
            convertRowIndexToModel(index)));
        servicoController.deleteServico(doghostPUEntityManager, servicoList,
            servicoTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelServicoSelector() {
        setSelectedServico(null);
        setVisible(false);
    }

    @Action(enabledProperty = "servicoRecordSelected")
    public void selectServico() {
        int index = servicoTable.getSelectedRow();
        setSelectedServico(servicoList.get(servicoTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterServico() {
        TableRowSorter servicoSorter = (TableRowSorter) servicoTable.getRowSorter();
        String text = servicoFilterTextField.getText();
        servicoFilterTextField.setText("");
        if(text.length()==0) {
            servicoSorter.setRowFilter(null);
        } else {
            servicoSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        servicoQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery("SELECT s FROM Servico s");
        servicoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(servicoQuery.getResultList());
        customCurrencyRenderer1 = new doghost.utils.CustomCurrencyRenderer();
        servicoFilterTextField = new javax.swing.JTextField();
        servicoFilterButton = new javax.swing.JButton();
        servicoSelectionPanel = new org.jdesktop.swingx.JXPanel();
        servicoScrollPane = new javax.swing.JScrollPane();
        servicoTable = new javax.swing.JTable();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        selectButton = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(ServicoSelector.class);
        customCurrencyRenderer1.setText(resourceMap.getString("customCurrencyRenderer1.text")); // NOI18N
        customCurrencyRenderer1.setName("customCurrencyRenderer1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        servicoFilterTextField.setName("servicoFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(ServicoSelector.class, this);
        servicoFilterButton.setAction(actionMap.get("filterServico")); // NOI18N
        servicoFilterButton.setName("servicoFilterButton"); // NOI18N

        servicoSelectionPanel.setName("servicoSelectionPanel"); // NOI18N

        servicoScrollPane.setName("servicoScrollPane"); // NOI18N

        servicoTable.setAutoCreateRowSorter(true);
        servicoTable.setName("servicoTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, servicoList, servicoTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
        columnBinding.setColumnName("Nome");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${valor}"));
        columnBinding.setColumnName("Valor");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${descricao}"));
        columnBinding.setColumnName("Descricao");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        servicoScrollPane.setViewportView(servicoTable);
        servicoTable.getColumnModel().getColumn(0).setMinWidth(150);
        servicoTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        servicoTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("servicoTable.columnModel.title0")); // NOI18N
        servicoTable.getColumnModel().getColumn(1).setMinWidth(60);
        servicoTable.getColumnModel().getColumn(1).setPreferredWidth(60);
        servicoTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("servicoTable.columnModel.title1")); // NOI18N
        servicoTable.getColumnModel().getColumn(1).setCellRenderer(customCurrencyRenderer1);
        servicoTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("servicoTable.columnModel.title2")); // NOI18N

        javax.swing.GroupLayout servicoSelectionPanelLayout = new javax.swing.GroupLayout(servicoSelectionPanel);
        servicoSelectionPanel.setLayout(servicoSelectionPanelLayout);
        servicoSelectionPanelLayout.setHorizontalGroup(
            servicoSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 641, Short.MAX_VALUE)
            .addGroup(servicoSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(servicoSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(servicoScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        servicoSelectionPanelLayout.setVerticalGroup(
            servicoSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 239, Short.MAX_VALUE)
            .addGroup(servicoSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(servicoSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(servicoScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        newButton.setAction(actionMap.get("newServico")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editServico")); // NOI18N
        editButton.setText(resourceMap.getString("editButton.text")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteServico")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelServicoSelector")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        selectButton.setAction(actionMap.get("selectServico")); // NOI18N
        selectButton.setName("selectButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(servicoFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(servicoFilterButton))
                            .addComponent(servicoSelectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 344, Short.MAX_VALUE)
                        .addComponent(selectButton)
                        .addGap(23, 23, 23))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(servicoFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(servicoFilterButton))
                .addGap(18, 18, 18)
                .addComponent(servicoSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton)
                    .addComponent(editButton)
                    .addComponent(deleteButton)
                    .addComponent(cancelButton)
                    .addComponent(selectButton))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ServicoSelector dialog = new ServicoSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private doghost.utils.CustomCurrencyRenderer customCurrencyRenderer1;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton selectButton;
    private javax.swing.JButton servicoFilterButton;
    private javax.swing.JTextField servicoFilterTextField;
    private java.util.List<doghost.entities.Servico> servicoList;
    private javax.persistence.Query servicoQuery;
    private javax.swing.JScrollPane servicoScrollPane;
    private org.jdesktop.swingx.JXPanel servicoSelectionPanel;
    private javax.swing.JTable servicoTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
